package com.imooc.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.bo.CreateAdminInfoBO;
import com.imooc.exception.GraceException;
import com.imooc.mapper.AdminMapper;
import com.imooc.pojo.Admin;
import com.imooc.result.ResponseStatusEnum;
import com.imooc.service.AdminInfoService;
import com.imooc.utils.MD5Utils;
import com.imooc.utils.PagedGridResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author liansen
 * @create 02-13-11:43
 */
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminInfoService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void createAdminInfo(CreateAdminInfoBO adminInfoBO) {

        //根据用户传来的用户名查询数据库，如果存在，则抛出一个错误信息
        LambdaQueryWrapper<Admin> wapper = new LambdaQueryWrapper<Admin>().eq(Admin::getUsername, adminInfoBO.getUsername());
        Admin admin = adminMapper.selectOne(wapper);
        if(admin != null){
            //管理员已存在
            GraceException.display(ResponseStatusEnum.ADMIN_USERNAME_EXIST_ERROR);
        }
        Admin adminInfo = new Admin();
        BeanUtils.copyProperties(adminInfoBO, adminInfo);
        //生成随机盐值(4位随机数)
        String slat = (int) ((Math.random()*9+1)*1000) + "";
        String newPassword = MD5Utils.encrypt(adminInfoBO.getPassword(), slat);
        adminInfo.setPassword(newPassword);

        adminInfo.setSlat(slat);
        adminInfo.setCreateTime(LocalDateTime.now());
        adminInfo.setUpdatedTime(LocalDateTime.now());
        adminMapper.insert(adminInfo);
    }

    @Override
    public PagedGridResult queryPage(Integer page, Integer limit, String accountName) {
        PageHelper.startPage(page, limit);
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<Admin>();
        //这里判空是因为用户输入空格的话, 参数会从'%%'变成'%   %',查询无果，所以添加判空
        if(StringUtils.isNotBlank(accountName)){
            wrapper.like(Admin::getUsername, accountName);
        }
        List<Admin> list = adminMapper.selectList(wrapper);
        PageInfo<Admin> pageInfo = new PageInfo<>(list);
        PagedGridResult result = new PagedGridResult();
        result.setRows(pageInfo.getList());
        result.setPage(page);
        result.setTotal(pageInfo.getPages());
        result.setRecords(pageInfo.getTotal());
        return result;
    }

    @Override
    public PagedGridResult queryPageByPlus(Integer page, Integer limit, String accountName) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<Admin>().like(Admin::getUsername, accountName);
        Page<Admin> itemPage = new Page<>(page, limit);
        Page<Admin> adminPage = adminMapper.selectPage(itemPage, wrapper);
        PagedGridResult pagedGridResult = new PagedGridResult();
        pagedGridResult.setPage(page);
        pagedGridResult.setLimit(limit);
        pagedGridResult.setRows(adminPage.getRecords()); //每页显示的内容
        pagedGridResult.setRecords(adminPage.getTotal()); //总记录数
        pagedGridResult.setTotal(adminPage.getCurrent()); //总页数
        return pagedGridResult;
    }

    @Override
    public void deleteAdmin(String username) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<Admin>()
                                            .eq(Admin::getUsername, username)
                                            .ne(Admin::getUsername, "admin");
        int res = adminMapper.delete(wrapper);
        if(res == 0){
            GraceException.display(ResponseStatusEnum.ADMIN_DELETE_ERROR);
        }
    }

}
